﻿using System;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.SessionState;
using Comsenz.Base;
using Lbb.Web;

namespace Comsenz.UCenter.Client.API
{
    /// <summary>
    /// uc 的摘要说明
    /// </summary>
    /// <author>Chiron</author>
    public class uc : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            if (context == null) return;

            var Request = context.Request;
            var Response = context.Response;
            var Session = context.Session;
            var Server = context.Server;

            if (!Comsenz.UCenter.Configs.UCenterConfig.Instance.Enabled)
            {//此功能被关闭.
                Response.StatusCode = 404;
                return;
            }

            var code = Request["code"];
            if (string.IsNullOrEmpty(code))
            {//错误的请求.
                Response.Write("Invalid Request");
                return;
            }

            try
            {
                var reqArgsStr = UcSyncHelper.uc_authcode(code);
                var reqArgs = HttpUtil.GetQueryParameters(reqArgsStr);
                if (reqArgs.Count == 0)
                {//未授权访问.
                    Response.Write("Unauthorized Access");
                    return;
                }

                var timeNow = PhpFunc.Time();

                int reqTime;
                int.TryParse(reqArgs.Get("time"), out reqTime);
                if (timeNow - reqTime > 3600)
                {//过期的请求.
                    Response.Write("Authracation has expiried");
                    return;
                }

                var action = (reqArgs.Get("action") ?? string.Empty).ToLower();
                var methodInfo = typeof(IClientAPI).GetMethod(action, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (methodInfo != null)
                {//执行操作.
                    dynamic reqObj = null;
                    var inputStr = new StreamReader(Request.InputStream).ReadToEnd();
                    if (!string.IsNullOrEmpty(inputStr))
                    {
                        reqObj = XmlHelper.UnSerialize(inputStr);
                    }

                    IClientAPI clientApi = new AspNetClientAPI();
                    Response.Write((int)methodInfo.Invoke(clientApi, new object[] { reqArgs, reqObj }));
                }
                else
                {//请求了未提供的操作.
                    Response.Write((int)ClientApiReturns.Failed);
                }

            }
            catch (Exception ex)
            {
                Response.Write((int)ClientApiReturns.Failed);
                //LogHelper.Append(ex);
                #region Debug
#if DEBUG
                throw ex;
#endif
                #endregion Debug
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}